import React from 'react';
import hashHistory from '@hashHistory';
import { Button } from 'antd';
import { autorun } from 'mobx';
import home2Store from './home2Store';
import styles from './index.module.less';

class AnyCom extends React.Component {
  state = {
    storeValue: home2Store.value,
  };

  disposer: any = null;

  componentDidMount() {
    this.disposer = autorun(() => {
      const { value } = home2Store;
      this.setState({ storeValue: value });
    });
  }

  //  组件销毁时，必须清除监听 autorun
  componentWillUnmount() {
    if (this.disposer) {
      this.disposer();
    }
  }

  render() {
    const { storeValue } = this.state;
    return (
      <div>
        <div>全局任意组件监听： {storeValue}</div>
      </div>
    );
  }
}

export default AnyCom;
